package com.summer.controller.userManager;

import com.alibaba.fastjson.JSONObject;
import com.summer.controller.BaseController;
import com.summer.entity.PlatformRole;
import com.summer.entity.PlatformUser;
import com.summer.service.IPlateformRoleService;
import com.summer.service.IPlatformRoleAuthorityService;
import com.summer.util.CallBackResult;
import com.summer.vo.PlatformRoleAuthorityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 角色管理Controller
 */
@Slf4j
@RestController
@RequestMapping("api/role")
public class UserRoleController extends BaseController{

    @Resource
    private IPlateformRoleService plateformRoleService;

    @Resource
    private IPlatformRoleAuthorityService platformRoleAuthorityService;

    /**
     * 角色列表，用于添加用户
     * @return
     */
    @PostMapping("/findAll")
    public String findAll() {
        return CallBackResult.returnJson(plateformRoleService.findAll());
    }

    /**
     * 权限列表
     * @return
     */
    @PostMapping("/findByAuthority")
    public String findByAuthority() {
        return CallBackResult.returnJson(plateformRoleService.findByAuthority());
    }

    /**
     * 加载角色下的权限信息
     * @param jsonData
     * @return
     */
    @PostMapping("/findRoleAuthority")
    public String findRoleAuthority(@RequestBody String jsonData){
        log.info("【加载角色下的权限】角色权限jsonData={}", jsonData.toString());
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        String id= (String) params.get("id");
        List<PlatformRoleAuthorityVo> roleAuthority = platformRoleAuthorityService.findRoleAuthority(Integer.valueOf(id));
        if (roleAuthority==null||roleAuthority.size()<0){
            return CallBackResult.returnJson(999,"角色列表为空或不存在");
        }
        return CallBackResult.returnJson(roleAuthority);
    }

    /**
     * 添加角色并赋予权限
     * @param jsonData
     * @param request
     * @return
     */
    @PostMapping("/insertRole")
    public String insertRole(@RequestBody String jsonData, HttpServletRequest request){
        log.info("【添加角色】添加角色jsonData={}", jsonData.toString());
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        String ids= (String) params.get("authorityList");
        String roleName= (String) params.get("roleName");
        String status= (String) params.get("status");
        //获取当前登录用户
        PlatformUser platformUser=redisPlatformUser(request);

        PlatformRole platformRole =new PlatformRole();
        platformRole.setRoleSuperId(platformUser.getId());
        platformRole.setName(roleName);
        platformRole.setStatus(Integer.valueOf(status));
        try {
            //添加角色
            plateformRoleService.insert(platformRole);
            // 添加权限
            platformRoleAuthorityService.setAuthority(platformRole.getId(),ids);
            return CallBackResult.returnJson(CallBackResult.CREATED,CallBackResult.MSG_CREATED);
        }catch (Exception e){
            e.printStackTrace();
            return CallBackResult.returnJson(CallBackResult.INVALID_REQUEST,CallBackResult.MSG_INVALID_REQUEST);
        }
    }

    /**
     * 修改角色名称和权限
     * @param jsonData
     * @param request
     * @return
     */
    @PostMapping("/updateRole")
    public String updateRole(@RequestBody String jsonData, HttpServletRequest request){
        log.info("【修改角色】修改角色jsonData={}", jsonData.toString());
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        String ids= (String) params.get("authorityList");
        String id= (String) params.get("id");
        String roleName= (String) params.get("roleName");
        String status= (String) params.get("status");
        //获取当前登录用户信息
        PlatformUser platformUser=redisPlatformUser(request);
        //查询角色信息
        PlatformRole platformRole =plateformRoleService.findById(Integer.valueOf(id));
        platformRole.setRoleSuperId(platformUser.getId());
        platformRole.setName(roleName);
        platformRole.setStatus(Integer.valueOf(status));
        try {
            //修改角色
            plateformRoleService.update(platformRole);
            // 设置权限
            platformRoleAuthorityService.setAuthority(platformRole.getId(),ids);
            return CallBackResult.returnJson(CallBackResult.CREATED,CallBackResult.MSG_CREATED);
        }catch (Exception e){
            e.printStackTrace();
            return CallBackResult.returnJson(CallBackResult.INVALID_REQUEST,CallBackResult.MSG_INVALID_REQUEST);
        }
    }

    /**
     * 删除角色
     * @param jsonData
     * @param request
     * @return
     */
    @PostMapping("/deleteRole")
    public String deleteRole(@RequestBody String jsonData, HttpServletRequest request){
        log.info("【删除角色】删除角色jsonData={}", jsonData.toString());
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        String id= (String) params.get("id");
        //获取当前登录用户信息
        PlatformUser platformUser=redisPlatformUser(request);
        //查询角色信息
        PlatformRole platformRole =plateformRoleService.findById(Integer.valueOf(id));
        platformRole.setRoleSuperId(platformUser.getId());
        platformRole.setStatus(2);
        try {
            //修改角色
            plateformRoleService.update(platformRole);
            // 删除角色权限记录
            platformRoleAuthorityService.deleteByRoleId(platformRole.getId());
            return CallBackResult.returnJson(CallBackResult.NO_CONTENT_SUCCESS,CallBackResult.MSG_NO_CONTENT_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return CallBackResult.returnJson(CallBackResult.INVALID_REQUEST_FAIL,CallBackResult.MSG_INVALID_REQUEST_FAIL);
        }
    }

}
